package cn.iocoder.yudao.module.train.controller.admin.wxuser;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.train.controller.admin.wxuser.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.WxUser.WxUserDO;
import cn.iocoder.yudao.module.train.service.wxUser.WxUserService;

@Tag(name = "管理后台 - 微信用户")
@RestController
@RequestMapping("/train/wx-user")
@Validated
public class WxUserController {

    @Resource
    private WxUserService wxUserService;

    @PostMapping("/create")
    @Operation(summary = "创建微信用户")
    @PreAuthorize("@ss.hasPermission('train:wx-user:create')")
    public CommonResult<Long> createWxUser(@Valid @RequestBody WxUserSaveReqVO createReqVO) {
        return success(wxUserService.createWxUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新微信用户")
    @PreAuthorize("@ss.hasPermission('train:wx-user:update')")
    public CommonResult<Boolean> updateWxUser(@Valid @RequestBody WxUserSaveReqVO updateReqVO) {
        wxUserService.updateWxUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除微信用户")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('train:wx-user:delete')")
    public CommonResult<Boolean> deleteWxUser(@RequestParam("id") Long id) {
        wxUserService.deleteWxUser(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除微信用户")
                @PreAuthorize("@ss.hasPermission('train:wx-user:delete')")
    public CommonResult<Boolean> deleteWxUserList(@RequestParam("ids") List<Long> ids) {
        wxUserService.deleteWxUserListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得微信用户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('train:wx-user:query')")
    public CommonResult<WxUserRespVO> getWxUser(@RequestParam("id") Long id) {
        WxUserDO wxUser = wxUserService.getWxUser(id);
        return success(BeanUtils.toBean(wxUser, WxUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得微信用户分页")
    @PreAuthorize("@ss.hasPermission('train:wx-user:query')")
    public CommonResult<PageResult<WxUserRespVO>> getWxUserPage(@Valid WxUserPageReqVO pageReqVO) {
        PageResult<WxUserDO> pageResult = wxUserService.getWxUserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WxUserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出微信用户 Excel")
    @PreAuthorize("@ss.hasPermission('train:wx-user:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWxUserExcel(@Valid WxUserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WxUserDO> list = wxUserService.getWxUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "微信用户.xls", "数据", WxUserRespVO.class,
                        BeanUtils.toBean(list, WxUserRespVO.class));
    }

}
